package vn.ugame.model.dal;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import vn.ugame.exception.DataConnectionFailed;
import vn.ugame.exception.InvalidEntityClass;
import vn.ugame.model.entity.Module;

public class ModuleDA {

    public ModuleDA() {
    }

    public Module findModuleById(String id)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        Module module = null;
        List<Module> modules = findModules(id, null, null, null, null);
        if (modules.size() > 0) {
            module = modules.get(0);
        }
        return module;
    }

    public Module findModuleByName(String name)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        Module module = findLocalModuleByName(name);
        if (module == null) {
            List<Module> modules = findModules(null, name, null, null, null);
            if (modules.size() > 0) {
                module = modules.get(0);
            }
        }
        return module;
    }

    public Module findLocalModuleByName(String name)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        Module module = null;
        List<Module> modules = findLocalModules(null, name, null, null, null, null);
        if (modules.size() > 0) {
            module = modules.get(0);
        }
        return module;
    }

    public Module findLocalModule(String id, Locale locale)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        Module module = null;
        List<Module> modules = findLocalModules(id, null, null, null, null, locale);
        if (modules.size() > 0) {
            module = modules.get(0);
        }
        return module;
    }

    public List<Module> findModules(String id, String name, String text,
            Integer type, Boolean root)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        List<Module> moduleList = new ArrayList<Module>();

        Connection conn = ConnectionFactory.getInstance().createDBConnection();

        String query = "{ call findModules(?,?,?,?,?) }";
        CallableStatement cs = conn.prepareCall(query);
        cs.setString(1, id);
        cs.setString(2, name);
        cs.setString(3, text);
        if (type == null) {
            cs.setNull(4, Types.INTEGER);
        } else {
            cs.setInt(4, type.intValue());
        }
        if (root == null) {
            cs.setNull(5, Types.BIT);
        } else {
            cs.setBoolean(5, root.booleanValue());
        }

        ResultSet rs = cs.executeQuery();
        EntityFactory factory = EntityFactory.createEntityFactory();
        while (rs.next()) {
            Module module = factory.createEntity(rs, Module.class);
            moduleList.add(module);
        }
        rs.close();
        cs.close();
        conn.close();
        return moduleList;
    }

    public List<Module> findLocalModules(String id, String name, String text,
            Integer type, Boolean root, Locale locale)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {

        List<Module> moduleList = new ArrayList<Module>();

        Connection conn = ConnectionFactory.getInstance().createDBConnection();

        String query = "{ call findLocalModules(?,?,?,?,?,?) }";
        CallableStatement cs = conn.prepareCall(query);
        cs.setString(1, id);
        cs.setString(2, name);
        cs.setString(3, text);
        if (type == null) {
            cs.setNull(4, Types.INTEGER);
        } else {
            cs.setInt(4, type.intValue());
        }
        if (root == null) {
            cs.setNull(5, Types.BIT);
        } else {
            cs.setBoolean(5, root.booleanValue());
        }
        if (locale == null) {
            cs.setNull(6, Types.VARCHAR);
        } else {
            cs.setString(6, locale.getLanguage());
        }

        ResultSet rs = cs.executeQuery();
        EntityFactory factory = EntityFactory.createEntityFactory();
        while (rs.next()) {
            Module module = factory.createEntity(rs, Module.class);
            moduleList.add(module);
        }

        rs.close();
        cs.close();
        conn.close();

        return moduleList;
    }
}
